package com.phc.a20200511work.video;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.phc.a20200511work.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/**
 * 版权：没有版权 看得上就用
 *
 * @author peng
 * 创建日期：2020/5/11 20
 * 描述：
 */
public class MainActivityAdapter extends RecyclerView.Adapter<MainActivityAdapter.ViewHolder> {

    Context context ;
    ArrayList<String> title = new ArrayList<>();
    ArrayList<String> provider = new ArrayList<>();
    ArrayList<String> providerIcon = new ArrayList<>();
    ArrayList<String> cover = new ArrayList<>();
    ArrayList<String> replyCount = new ArrayList<>();
    ArrayList<String> collectionCount = new ArrayList<>();
    ArrayList<String> playUrl = new ArrayList<>();

    public MainActivityAdapter(Context context, ArrayList<String> title, ArrayList<String> provider, ArrayList<String> providerIcon, ArrayList<String> cover, ArrayList<String> replyCount, ArrayList<String> collectionCount, ArrayList<String> playUrl) {
        this.context = context;
        this.title = title;
        this.provider = provider;
        this.providerIcon = providerIcon;
        this.cover = cover;
        this.replyCount = replyCount;
        this.collectionCount = collectionCount;
        this.playUrl = playUrl;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.activity_main_recycler_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        holder.activityMainRecyclerItemTitle.setText(title.get(position));
        holder.activityMainRecyclerItemFrom.setText(provider.get(position));
        holder.activityMainRecyclerItemComment.setText("评论："+replyCount.get(position));
        holder.activityMainRecyclerItemLike.setText("喜欢："+collectionCount.get(position));
        //对视频进播放
        try {
            holder.activityMainRecyclerItemVideoView.setMediaController(new MediaController(context));
            holder.activityMainRecyclerItemVideoView.setVideoPath(playUrl.get(position));
//            holder.activityMainRecyclerItemVideoView.start();
            Picasso.get().load(providerIcon.get(position)).into(holder.activityMainRecyclerItemFromImage);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public int getItemCount() {
        return title.size();
    }



    public class ViewHolder extends RecyclerView.ViewHolder {
         VideoView activityMainRecyclerItemVideoView;
         TextView activityMainRecyclerItemTitle;
         ImageView activityMainRecyclerItemFromImage;
         TextView activityMainRecyclerItemFrom;
         TextView activityMainRecyclerItemComment;
         TextView activityMainRecyclerItemLike;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
                activityMainRecyclerItemVideoView = (VideoView) itemView.findViewById(R.id.activity_main_recycler_item_videoView);
                activityMainRecyclerItemTitle = (TextView) itemView.findViewById(R.id.activity_main_recycler_item_title);
                activityMainRecyclerItemFromImage = (ImageView) itemView.findViewById(R.id.activity_main_recycler_item_fromImage);
                activityMainRecyclerItemFrom = (TextView) itemView.findViewById(R.id.activity_main_recycler_item_from);
                activityMainRecyclerItemComment = (TextView) itemView.findViewById(R.id.activity_main_recycler_item_comment);
                activityMainRecyclerItemLike = (TextView) itemView.findViewById(R.id.activity_main_recycler_item_like);
        }
    }
}
